import axios from "axios";

//允许跨域携带cookie信息
axios.defaults.withCredentials = true;

axios.interceptors.request.use(
  config => {
      return config;
  },
  error => {
      return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  response => {
      if (response.status == 200) {
          return Promise.resolve(response);
      } else {
          return Promise.reject(response);
      }
  },
  error => {

      alert(JSON.stringify(error), '请求异常', {
          confirmButtonText: '确定',
          callback: (action) => {
              console.log(action)
          }
      });
  }
);
const httpRequest = {
  get(url,data){
    return new Promise((resolve, reject) => {
      axios.get(url,data).then(res => {
        resolve(res.data)
      }).catch(err => {
        reject(err)
      })
    })
  },
  post(url ,data){
    return new Promise((resolve, reject) => {
      axios.post(url,data).then(res => {
        resolve(res.data)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export {
  httpRequest
}